//
//  String+Ex.swift
//  
//
//  Created by tank on 2021/12/17.
//

import Foundation
import LibBase

public extension Extension where Base == String {
    private var apiBaseUrl: String? {
        return HttpService.shared.apiBaseUrl
    }
    
    private var h5BaseUrl: String? {
        return HttpService.shared.h5BaseUrl
    }
    
    var absoluteApiUrl: String {
        
        return absoluteUrl(baseUrl: apiBaseUrl)
    }
    
    var absoluteH5Url: String {
        
        return absoluteUrl(baseUrl: h5BaseUrl)
    }
    
    private func absoluteUrl(baseUrl: String?) -> String {
        guard let baseUrl = baseUrl else {
            return base
        }
        
        guard let urlComponents = URLComponents(string: base.ex.isUrlEncoded ? base : base.ex.encodeURI() ?? base),
              urlComponents.host == nil else {
            return base
        }
        
        return base.first == "/" ? "\(baseUrl)\(base)" : "\(baseUrl)/\(base)"
    }
}
